<?php
/**
 * @特惠赚、商品分类控制器API
 * Author: 小谢
 * Date: 2019-5-21
 */
namespace app\api\controller;
use app\api\model\Goods;
use app\api\logic\PageLogic;

class Category extends Base
{
  
    /**
     *  获取分类对应商品列表
     */
    public function goodsList() {
        $cate_id = I('get.cate_id');
        if(!is_numeric($cate_id)) {
            $this->ajaxReturn($this->callBack(0,0,'cate_id参数错误',self::emptyObject()),'JSON');
        }

        $condition['level'] = 3;
        $categorys = M('goods_category')->whereLike('parent_id_path','%'.'_'.$cate_id.'_'.'%')->where($condition)->field('id,parent_id,name,parent_id_path')->select();

        //取出第三层分类
        if($categorys) {
            foreach ($categorys as $key => $v) {
                $ids[] = $v['id'];
            }
        }

        //分页参数
        $page = I('post.page') ? intval(I('post.page')) : 1 ;
        $pageSize = 20;
        $count = M('goods')->whereIn('cate_id',$ids)->count();
        $PageLogic = new PageLogic($page,$pageSize,$count);
        $pageInfo = $PageLogic->pageHandle();//分页操作
        $goodsList = M('goods')->whereIn('cate_id',$ids)->order('add_time DESC')->limit($pageInfo['pageStart'],$pageInfo['pageSize'])->select();
        if($goodsList) {
             $this->ajaxReturn($this->callBack(1,0,'成功',['page'=>$page,'pageSize'=>$pageSize,'totalPage'=>$pageInfo['totalPage'],'list'=>$goodsList]),'JSON'); 
        }
        $this->ajaxReturn($this->callBack(0,0,'暂无商品数据',self::emptyObject()),'JSON');
    }

}
